package io.renren.common.utils;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;


/**
 * @author: Mr.Wang
 * @description:
 * @create: $ $
 **/
public class HMAC_SHA1 {

    //北京金诺的头条账户
    //private static final String key = "NjU3STc3SzZKOEE3";
    //private static final String token = "3c836e2d7125aa2c1da2787728d576d302219754";

    //青岛金诺的头条账户
    private static final String key = "QTZIMlVSMThUN0RR";
    private static final String token = "2cc3254b31139a2b792af74d28d5615d162d091f";

    /**
     * 将加密后的字节数组转换成字符串
     *
     * @param b 字节数组
     * @return 字符串
     */
    private static String byteArrayToHexString(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b!=null && n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1) {
                hs.append('0');
            }
            hs.append(stmp);
        }
        return hs.toString().toLowerCase();
    }
    /**
     * sha256_HMAC加密
     * @param message 消息
     * @param secret  秘钥
     * @return 加密后字符串
     */
    public static String sha256_HMAC(String message, String secret) {
        String hash = "";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
            hash = byteArrayToHexString(bytes);
            System.out.println(hash);
            hash = Base64.encodeBase64String(hash.getBytes());
            System.out.println(hash);
        } catch (Exception e) {
            System.out.println("Error HmacSHA256 ===========" + e.getMessage());
        }
        return hash;
    }



    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        Long timestap = System.currentTimeMillis() / 1000;
        String startTime = "2019-05-01";
        String endTime = "2019-07-18";
        String size = "10";
        String page = "1";
        String data = "/crm/v2/openapi/pull-clues/?start_time="+startTime+"&end_time="+endTime+" " + timestap;
        System.out.println(data);
        String genHMAC = sha256_HMAC(data, key);

        Map<String, String> headers = new HashMap<>();
        headers.put("Timestamp", timestap + "");
        headers.put("Access-Token", token);
        headers.put("Content-Type", "application/json");
        headers.put("Signature", genHMAC);

        String params = "start_time="+startTime+"&end_time="+endTime+"&page_size="+size+"&page="+page;

        HttpClient client = new HttpClient();
        String str = client.sendGet("https://crm.bytedance.com/crm/v2/openapi/pull-clues/", params, headers);
        System.out.println(str);
        JSONObject obj = JSONObject.fromObject(str);
        String status = obj.getString("status");
        int times = 0;
        if("success".equals(status)){
            int count  = obj.getInt("count");
            System.out.println(count);
            if(count>10){
                times = count/10;
            }
            for(int i=0;i<times;i++){

            }
        }

    }


}
